package com.heima.behavior.service.impl;

import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP行为实体表,一个行为实体可能是用户或者设备，或者其它 服务实现类
 * </p>
 *
 * @author djc
 * @since 2022-10-12
 */
@Service
public class ApBehaviorEntryServiceImpl implements IApBehaviorEntryService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ApBehaviorEntry getEntryByUserIdOrEquipmentId(Integer userId, String equipmentId) {

        Query query = null;
        if (userId == 0) { //未登录 ，使用设备id
            query = new Query(Criteria.where("entryId").is(equipmentId.toString()));
        } else {
            query = new Query(Criteria.where("entryId").is(userId.toString()));
        }
        ApBehaviorEntry entry = mongoTemplate.findOne(query, ApBehaviorEntry.class);
//        ApBehaviorEntry entry = mongoTemplate.find(query, ApBehaviorEntry.class).stream().findFirst().orElse(null);
        if (entry == null) {  // 没有实体 ，应该新增一个
            entry = new ApBehaviorEntry();
            if (userId == 0) {
                entry.setType(0);   //实体类型            0终端设备            1用户
                entry.setEntryId(equipmentId);
            } else {
                entry.setType(1);   //实体类型            0终端设备            1用户
                entry.setEntryId(userId.toString());
            }
            entry.setCreatedTime(new Date());
            mongoTemplate.save(entry);
        }
        return entry;
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(BehaviorDto dto) {
//        {
//            "isfollow": true,
//                "islike": true,
//                "isunlike": false,
//                "iscollection": true
//        }
        Map map = new HashMap();
        map.put("isfollow", redisTemplate.hasKey("saveFollowBehavior" + dto.getUserId() + "_" + dto.getEquipmentId() + "_" + dto.getAuthorId() + "_" + dto.getArticleId()));
        map.put("islike", redisTemplate.hasKey("saveLikeBehavior" + dto.getUserId() + "_" + dto.getEquipmentId() + "_null_" + dto.getArticleId()));
        map.put("isunlike", redisTemplate.hasKey("saveUnlikesBehavior" + dto.getUserId() + "_" + dto.getEquipmentId() + "_null_" + dto.getArticleId()));
        map.put("iscollection", redisTemplate.hasKey("saveCollectionBehavior" + dto.getUserId() + "_" + dto.getEquipmentId() + "_null_" + dto.getArticleId()));
        return ResponseResult.okResult(map);
    }
}
